package com.centaline.androidsalesblog.activities.mine;

import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatEditText;
import android.view.View;
import android.widget.ScrollView;

import com.android.volley.VolleyError;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.api.bizcommon.UserFeedBackApi;
import com.centaline.androidsalesblog.base.BaseActivity;
import com.centaline.androidsalesblog.constants.AppConstant;
import com.centaline.androidsalesblog.model.responseobject.UserFeedBackRo;
import com.centanet.framework.interfaces.IResponseListener;
import com.jakewharton.rxbinding.view.RxView;
import com.jakewharton.rxbinding.widget.RxTextView;

import java.util.concurrent.TimeUnit;

import rx.functions.Action1;

/**
 * 意见反馈
 * <p>
 * Created by fusl1 on 2015/12/28.
 * Modify by Lihq on 2016/01/21
 */
public class OpinionActivity extends BaseActivity {

    private AppCompatEditText aet_opinion;
    private AppCompatButton btn_opinion;
    private ScrollView sv_sh;

    private UserFeedBackApi userFeedBackApi;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_opinion;
    }

    @Override
    protected void findViews() {
        setUniversalToolbar(R.string.title_opinion);
        aet_opinion = (AppCompatEditText) findViewById(R.id.aet_opinion);
        btn_opinion = (AppCompatButton) findViewById(R.id.btn_opinion);
        sv_sh = (ScrollView) findViewById(R.id.sv_sh);
    }

    @Override
    protected void initViews() {
        RxTextView
                .textChanges(aet_opinion)
                .subscribe(new Action1<CharSequence>() {
                    @Override
                    public void call(CharSequence charSequence) {
                        btn_opinion.setEnabled(charSequence.length() > 4);
                    }
                });
        userFeedBackApi = new UserFeedBackApi(OpinionActivity.class, UserFeedBackRo.class, new IResponseListener<UserFeedBackRo>() {
            @Override
            public void onResponse(UserFeedBackRo userFeedBackRo) {
                if (userFeedBackRo.getResultNo() == 0) {
                    toast("非常感谢您的反馈");
                    finish();
                } else {
                    snack(userFeedBackRo.getRMessage());
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                netWorkUnable();
            }
        });
        RxView.clicks(btn_opinion)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        request();
                    }
                });
        if ("021".equals(AppConstant.CityCode)) {
            sv_sh.setVisibility(View.VISIBLE);
        }
    }

    private void request() {
        hideKeyboard(aet_opinion);
        userFeedBackApi.setFeedBack(aet_opinion.getText().toString());
        userFeedBackApi.startRequest();
    }
}

